package com.cincc.cinccimdemo.ui.contact;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.GroupListAdapter;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.ui.conversation.ConversationActivity;
import com.cincc.cinccimdemo.viewmodel.GroupListViewModel;
import com.cincc.im.model.enums.ImSendType;
import com.cincc.im.table.IMContactGroup;

public class GroupListActivity extends IMBaseActivity {

    @Override
    protected int contentLayout() {
        return R.layout.activity_group_list;
    }

    LinearLayout groupsLinearLayout;
    RecyclerView recyclerView;
    TextView tipTextView;

    GroupListAdapter groupListAdapter;

    @Override
    protected void bindViews() {
        super.bindViews();
        groupsLinearLayout = findViewById(R.id.groupsLinearLayout);
        recyclerView = findViewById(R.id.groupRecyclerView);
        tipTextView = findViewById(R.id.tipTextView);
    }

    GroupListViewModel groupListViewModel;

    @Override
    protected void afterViews() {
        super.afterViews();
        groupListAdapter = new GroupListAdapter(this);
        recyclerView.setAdapter(groupListAdapter);
        groupListViewModel = new ViewModelProvider(this).get(GroupListViewModel.class);
        groupListViewModel.conversationListLiveData().observe(this, groups -> {
            if (!groups.isEmpty()) {
                groupListAdapter.setGroupList(groups);
            } else {
                groupsLinearLayout.setVisibility(View.GONE);
                tipTextView.setVisibility(View.VISIBLE);
            }
        });
        groupListAdapter.setOnDataClickItem(index -> openConversation(groupListAdapter.getItem(index)));
    }

    private void openConversation(IMContactGroup imContactGroup) {
        ConversationActivity.startGroupConversation(this, imContactGroup.groupId, imContactGroup.name);
    }
}
